package com.yongninger.dao;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AbstractDao 
{
	private Context mContext;
	private SQLiteDatabase db;

	public AbstractDao(Context context) 
	{
		mContext = context;
		db = new DBHelper(mContext, "forever", null, 1).getWritableDatabase();
	}

	protected void execute(String sql) throws SQLException 
	{
		db.execSQL(sql);
	}

	protected int delete(String table, String whereClause, String[] whereArgs) 
	{
		
		int result = db.delete(table, whereClause, whereArgs);
		return result;
	}

	protected long insert(String table, String nullColumnHack,ContentValues values) throws SQLException 
	{
		long result = db.insertOrThrow(table, nullColumnHack, values);
		return result;
	}

	protected int update(String table, ContentValues values,String whereClause, String[] whereArgs) 
	{
		int result = db.update(table, values, whereClause, whereArgs);
		return result;
	}

	protected Cursor query(String sql, String[] selectionArgs) 
	{
		return db.rawQuery(sql, selectionArgs);
	}

	protected Cursor query(String table, String[] columns, String selection, String[] selectionArgs) 
	{
		return db.query(table, columns, selection, selectionArgs, null, null,null);
	}
	
	protected Cursor query(String table, String[] columns, String selection, String[] selectionArgs,String groupBy, String having, String orderBy) 
	{
		return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
		
	}
	



	public void dispose() 
	{
		synchronized (db) 
		{
			if (db != null && db.isOpen())
				db.close();
		}
	}
	
	
	public void beginTransaction() {
		db.beginTransaction();
	}
	
	public void commit() {
		db.setTransactionSuccessful();
		db.endTransaction();
	}
	
	protected void rollback() {
		db.endTransaction();
	}
	
}
